package com.guanghua.brick.html;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.guanghua.brick.html.tag.WebAppContextTag;

public class FileControl extends WebControl {

	//private static Log logger = LogFactory.getLog(SelectControl.class);
	
	private String textProperty = "text";
	private String valueProperty = "value";
	private String url = "/fileupload";
	private int size = 1;
	
	private String objectId = null;
	private String objectType = null;
	private String beanId = null;
	private boolean readOnly = false;
	
	public String buildHTML(HttpServletRequest request,
			HttpServletResponse response) {
		StringBuffer html = new StringBuffer();
		
		html.append("<input id=\"")
		.append(this.getId())
		.append("\" type=\"hidden\" name=\"")
		.append(this.getName())
		.append("\" textProperty=\"")
		.append(this.textProperty)
		.append("\" valueProperty=\"")
		.append(this.valueProperty)
		.append("\" url=\"")
		.append(this.url)
		.append("\"");
		
		if (this.objectId != null)
			html.append(" objectId=\"").append(this.objectId).append("\"");
		if (this.objectType != null)
			html.append(" objectType=\"").append(this.objectType).append("\"");
		if (this.beanId != null)
			html.append(" beanId=\"").append(this.beanId).append("\"");
		
		
		html.append(">");
			
		html.append("<select id=\"")
		.append(this.getId())
		.append(".list\" size=\"")
		.append(this.size)
		.append("\" ")
		.append(this.getHtml())
		.append("></select>\n");

		html.append("<img id=\"").append(this.getId()).append(".button.download\" src=\"").append(WebAppContextTag.getWebAppContext(request)).append("/brick/image/download.gif\" class=\"img_button\" title=\"下载\"/>\n");
		if (!readOnly)
			html.append("<img id=\"").append(this.getId()).append(".button.upload\" src=\"").append(WebAppContextTag.getWebAppContext(request)).append("/brick/image/upload.gif\" class=\"img_button\" title=\"上传\"/>\n");
		if (!readOnly)
			html.append("<img id=\"").append(this.getId()).append(".button.remove\" src=\"").append(WebAppContextTag.getWebAppContext(request)).append("/brick/image/delete.gif\" class=\"img_button\" title=\"删除\"/>\n");
		
		//scripts生成
		html.append("<script language=\"javascript\">\n");
		html.append("	var ")
			.append(this.getId())
			.append(" = new HtmlFileUploader(\"")
			.append(this.getId())
			.append("\");\n");
		
		html.append("</script>\n");
		
		return html.toString();
	}

	public String getObjectId() {
		return objectId;
	}

	public void setObjectId(String objectId) {
		this.objectId = objectId;
	}

	public String getObjectType() {
		return objectType;
	}

	public void setObjectType(String objectType) {
		this.objectType = objectType;
	}

	public String getTextProperty() {
		return textProperty;
	}

	public void setTextProperty(String textProperty) {
		this.textProperty = textProperty;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getValueProperty() {
		return valueProperty;
	}

	public void setValueProperty(String valueProperty) {
		this.valueProperty = valueProperty;
	}

	public boolean isReadOnly() {
		return readOnly;
	}

	public void setReadOnly(boolean readOnly) {
		this.readOnly = readOnly;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public String getBeanId() {
		return beanId;
	}

	public void setBeanId(String beanId) {
		this.beanId = beanId;
	}

}
